package com.gjy.concurrency.aotmic;

import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-03-31 15:34:24
 */
public class Atomic5Test {

    @Test
    public void test1() {
        LongAdder adder = new LongAdder();

        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 1; i <= 10000000; i++) {
            int finalI = i;
            pool.execute(() -> adder.add(finalI));
        }

        long sum = adder.sum();
        System.out.println("sum = " + sum);
    }

    @Test
    public void test2() {
        AtomicLong al = new AtomicLong();

        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 1; i <= 10000000; i++) {
            int finalI = i;
            pool.execute(() -> al.addAndGet(finalI));
        }

        System.out.println("al.get() = " + al.get());
    }
}
